﻿using Gzh.BLL.Infrastructure;
using Gzh.Core.Constant;
using Gzh.DAL.Models;
using Gzh.Lib;
using Gzh.Model;
using Gzh.UI.API.Filter;
using Gzh.UI.API.Helpers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace Gzh.UI.API.Controllers
{
    /// <summary>
    /// ${table.comment}
    /// </summary>
    [NoApiAuth]
    public class ${ClassName}Controller : BaseApiController
    {

        public I${ClassName}Contract ${ClassName}Contract { get; set; }

        /// <summary>
        /// 列表(+_+)
        /// </summary>
        /// <param name="insId"></param>
        /// <param name="name"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        [HttpGet]
        public OperationResult Page(string page = "1", string rows = "10")
        {
            var result = ValidateHelper.IsNotNullOrEmpty( page, rows);
            if (result.ResultType != OperationResultType.Success)
            {
                return result;
            }

            result = ValidateHelper.IsInt32(page, rows);
            if (result.ResultType != OperationResultType.Success)
            {
                return result;
            }
            //其它参数过滤



            int total = 0;
            int records = 0;

            var data = ${ClassName}Contract.Page(page.ToInt32(),rows.ToInt32(), ref total, ref records);
            return new OperationResult(OperationResultType.Success, new PagingVModel(total, records, data));
        }





        /// <summary>
        /// 详情(+_+)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet]
        public OperationResult GetById(string id)
        {

            var result = ValidateHelper.IsNotNullOrEmpty(id);
            if (result.ResultType != OperationResultType.Success)
            {
                return result;
            }

            result = ValidateHelper.IsInt64(id);
            if (result.ResultType != OperationResultType.Success)
            {
                return result;
            }


            var data = ${ClassName}Contract.GetInfoById(id.ToInt64());

            return new OperationResult(OperationResultType.Success, data);

        }
        

        /// <summary>
        /// 添加(+_+)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public OperationResult Add(JObject model)
        {
            if (model == null)
            {
                return new OperationResult(OperationResultType.ParamNull);
            }
           <#list table.columns  as column>
           <#if column.columnKey!='true'>
            var ${column.columnName2} = model.Value<string>("${column.columnName2}");  //${column.columnComment}
           </#if>
           </#list>
            var operatorId = model.Value<string>("operatorId");  //操作者ID
            var data = new ${TableName}()
            {
           <#list table.columns  as column>
           <#if column.columnKey!='true'>
            <#if column_has_next>
             <#if column.columnType == "long" && column.isRequired =='true'>
                ${column.columnName} = ${column.columnName2}.ToInt64(),//${column.columnComment}
             <#elseif column.columnType == "long" && column.isRequired =='false'>
                ${column.columnName} = ${column.columnName2}.StringToInt64(),//${column.columnComment}
             <#elseif column.columnType == "int" && column.isRequired =='true'>
                ${column.columnName} = ${column.columnName2}.ToInt32(),//${column.columnComment}
             <#elseif column.columnType == "int" && column.isRequired =='false'>
                ${column.columnName} = ${column.columnName2}.StringToInt32(),//${column.columnComment}
             <#elseif column.columnType == "DateTime">
                ${column.columnName} = ${column.columnName2}.ToDateTime(),//${column.columnComment}
             <#elseif column.columnType == "boolean">
                ${column.columnName} = ${column.columnName2}.StringToInt32().ToBool(),//${column.columnComment}
              <#else>
                ${column.columnName} = ${column.columnName2},//${column.columnComment}
             </#if>
            <#else>
              <#if column.columnType == "long" && column.isRequired =='true'>
                 ${column.columnName} = ${column.columnName2}.ToInt64(),//${column.columnComment}
              <#elseif column.columnType == "long" && column.isRequired =='false'>
                 ${column.columnName} = ${column.columnName2}.StringToInt64(),//${column.columnComment}
              <#elseif column.columnType == "int" && column.isRequired =='true'>
                 ${column.columnName} = ${column.columnName2}.ToInt32(),//${column.columnComment}
              <#elseif column.columnType == "int" && column.isRequired =='false'>
                 ${column.columnName} = ${column.columnName2}.StringToInt32(),//${column.columnComment}
              <#elseif column.columnType == "DateTime">
                 ${column.columnName} = ${column.columnName2}.ToDateTime(),//${column.columnComment}
              <#elseif column.columnType == "boolean">
                 ${column.columnName} = ${column.columnName2}.StringToInt32().ToBool(),//${column.columnComment}
                <#else>
                 ${column.columnName} = ${column.columnName2}//${column.columnComment}
               </#if>
            </#if>
          </#if>
           </#list>
                create_dt = DateTime.Now,//创建时间
                create_user_id = operatorId.ToInt64(),//创建者
            };
            return ${ClassName}Contract.AddInfo(data);
        }



        //修改        
        [HttpPost]
        public OperationResult Update(JObject model)
        {
            if (model == null)
            {
                return new OperationResult(OperationResultType.ParamNull);
            }
          <#list table.columns  as column>
            var ${column.columnName2} = model.Value<string>("${column.columnName2}");  //${column.columnComment}
           </#list>
            var operatorId = model.Value<string>("operatorId");  //操作者ID
            var result = ValidateHelper.IsNotNullOrEmpty();
            if (result.ResultType != OperationResultType.Success)
            {
                return result;
            }

            result = ValidateHelper.IsInt64();
            if (result.ResultType != OperationResultType.Success)
            {
                return result;
            }

            var data = new ${TableName}()
            {
              <#list table.columns  as column>
              <#if column_has_next>
              <#if column.columnType == "long" && column.isRequired =='true'>
                 ${column.columnName} = ${column.columnName2}.ToInt64(),//${column.columnComment}
              <#elseif column.columnType == "long" && column.isRequired =='false'>
                 ${column.columnName} = ${column.columnName2}.StringToInt64(),//${column.columnComment}
              <#elseif column.columnType == "int" && column.isRequired =='true'>
                 ${column.columnName} = ${column.columnName2}.ToInt32(),//${column.columnComment}
              <#elseif column.columnType == "int" && column.isRequired =='false'>
                 ${column.columnName} = ${column.columnName2}.StringToInt32(),//${column.columnComment}
              <#elseif column.columnType == "DateTime">
                 ${column.columnName} = ${column.columnName2}.ToDateTime(),//${column.columnComment}
              <#elseif column.columnType == "boolean">
                 ${column.columnName} = ${column.columnName2}.StringToInt32().ToBool(),//${column.columnComment}
                <#else>
                 ${column.columnName} = ${column.columnName2},//${column.columnComment}
               </#if>
              <#else>
                <#if column.columnType == "long" && column.isRequired =='true'>
                  ${column.columnName} = ${column.columnName2}.ToInt64(),//${column.columnComment}
                <#elseif column.columnType == "long" && column.isRequired =='false'>
                  ${column.columnName} = ${column.columnName2}.StringToInt64(),//${column.columnComment}
                <#elseif column.columnType == "int" && column.isRequired =='true'>
                  ${column.columnName} = ${column.columnName2}.ToInt32(),//${column.columnComment}
                <#elseif column.columnType == "int" && column.isRequired =='false'>
                  ${column.columnName} = ${column.columnName2}.StringToInt32(),//${column.columnComment}
                <#elseif column.columnType == "DateTime">
                  ${column.columnName} = ${column.columnName2}.ToDateTime()//${column.columnComment}
                <#elseif column.columnType == "boolean">
                  ${column.columnName} = ${column.columnName2}.StringToInt32().ToBool(),//${column.columnComment}
                  <#else>
                  ${column.columnName} = ${column.columnName2}//${column.columnComment}
                 </#if>
              </#if>
             </#list>
                 update_dt =DateTime.Now,//修改时间
                 update_user_id = operatorId.ToInt64(),//修改者
            };
            return ${ClassName}Contract.UpdateInfo(data);
        }


        //--------------------------------------------------------------------------------------------------------------------------------------------- 


        //删除
        [HttpPost]
        public OperationResult DeleteById(JObject model)
        {
            if (model == null)
            {
                return new OperationResult(OperationResultType.ParamNull);
            }
            var id = model.Value<string>("id");
            var operatorId = model.Value<string>("operatorId");

            var result = ValidateHelper.IsNotNullOrEmpty(id, operatorId);
            if (result.ResultType != OperationResultType.Success)
            {
                return result;
            }

            result = ValidateHelper.IsInt64(id, operatorId);
            if (result.ResultType != OperationResultType.Success)
            {
                return result;
            }


            return ${ClassName}Contract.DeleteById(id.ToInt64(), operatorId.ToInt64());
        }



        //--------------------------------------------------------------------------------------------------------------------------------------------- 


    }
}